home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 1995 #5 & #6 / Amiga Plus CD - 1995 - No. 5 and 6.iso / pd / packer / mcvert / mcvert.c < prev    next >
C/C++ Source or Header  |  1995-05-08  |  14KB  |  393 lines

  1. /* mcvert.c - version 1.05 - 10 January, 1990
  2.  * Written by Doug Moore - Rice University - dougm@rice.edu - April '87
  3.  * Sun bug fixes, assorted stuff - Jim Sasaki, March '89
  4.  * Changed default max_line_size from 2000 to unlimited - Doug Moore, April, '89
  5.  * Sun 3/60 doesn't like odd-sized structs.  Bug fixed - Doug Moore, April, '89
  6.  *                                              - aided by Spencer W. Thomas
  7.  * Didn't handle properly many hqx files combined in one file.  Bug fixed -
  8.  *                                           Doug Moore, June, '89
  9.  * Modified to handle MacBinaryII specification. Jim Van Verth, Sept, '89
  10.  *
  11.  * Fixed a bug when there are blank lines in hqx data, as happens when newline
  12.  * get translated to CRLF and then to \n\n, common for some file transfers.
  13.  * The last hqx line would be lost if the previous line was blank or junk.
  14.  *    Glenn Trewitt, Stanford University, 1990    (1.05)
  15.  *
  16.  * Fixed a bug that occurred when extracting data or resource
  17.  * forks on a Sun 4.  It was a byte alignment problem.
  18.  * Rick Zaccone, zaccone@bucknell.edu.  April 1991.  Version 1.6
  19.  * 
  20.  * Fixed:
  21.  *   Sent all "Converting ... " lines to stdout instead of stderr
  22.  *   Changed mactypes.h for HP-UX systems
  23.  *      Alan Danziger, aland@cs.brandeis.edu.  October 1991. Version 1.6.5
  24.  *
  25.  * This program may be freely distributed for non-profit purposes.  It may not
  26.  * be sold, by itself or as part of a collection of software.  It may be freely
  27.  * modified as long as no modified version is distributed.  Modifications of
  28.  * interest to all can be incorporated into the program by sending them to me
  29.  * for distribution.  Parts of the code can be used in other programs.  I am not
  30.  * responsible for any damage caused by this program.  I hope you enjoy it.
  31.  */
  32.  
  33. #include "mactypes.h"
  34.  
  35. #define HQX 0
  36. #define TEXT 1
  37. #define DATA 2
  38. #define RSRC 3
  39. #define HOST 4
  40. #define FORWARDS 0
  41. #define BACKWARDS 1
  42.  
  43.      /* 24-Oct-91: Use verbose for everything except "Converting..." lines (typically stderr) */
  44. FILE *verbose;
  45.      /* 24-Oct-91: Use output for "Converting..." lines -- Spit them to stdout */
  46. FILE *output;
  47. char **hqxnames, **hqxnames_left;
  48. char *dir, *ext, *text_author;
  49. char *maxlines_str;
  50. int maxlines;
  51.  
  52. main(argc, argv)
  53. int argc;
  54. char **argv;
  55. {   char *flags, *getenv();
  56.     int direction, mode, unpit_flag;
  57.  
  58.     argv++;
  59.     argc--;
  60.     verbose = stderr;
  61.     output = stdout;
  62.     direction = FORWARDS;
  63.     mode = HQX;
  64.     unpit_flag = 0;
  65.  
  66.     if ((text_author = getenv("MAC_EDITOR"))    == NULL)    text_author = "MACA";
  67.     if ((ext =         getenv("MAC_EXT"))       == NULL)    ext = ".bin";
  68.     if ((dir =         getenv("MAC_DLOAD_DIR")) == NULL)    dir = ".";
  69.     if ((maxlines_str = getenv("MAC_LINE_LIMIT")) == NULL)  maxlines = 0;
  70.     else                                         maxlines = atoi(maxlines_str);
  71.     
  72.     /* Make command line arguments globally accessible */
  73.     hqxnames = (char **) calloc(argc+1, sizeof(char *));
  74.     if (hqxnames == NULL) error("no mem");
  75.     hqxnames_left = hqxnames;
  76.     while (argc--)  *hqxnames_left++ = *argv++;
  77.     *hqxnames_left = "-";
  78.     hqxnames_left = hqxnames;
  79.  
  80.     while (strcmp(*hqxnames_left, "-")) {
  81.         if (hqxnames_left[0][0] == '-') {
  82.             flags = *hqxnames_left++;
  83.             while (*++flags)
  84.                 switch (*flags) {
  85.                 case 'x':
  86.                     mode = HQX;
  87.                     break;
  88.                 case 'u':
  89.                     mode = TEXT;
  90.                     break;
  91.                 case 'd':
  92.                     mode = DATA;
  93.                     break;
  94.                 case 'r':
  95.                     mode = RSRC;
  96.                     break;
  97.                 case 'h':
  98.                     mode = HOST;
  99.                     break;
  100.                 case 'D':
  101.                     direction = FORWARDS;
  102.                     break;
  103.                 case 'U':
  104.                     direction = BACKWARDS;
  105.                     break;
  106.                 case 'q':
  107.                     unpit_flag = 0;
  108.                     break;
  109.                 case 'p':
  110.                     unpit_flag = 1;
  111.                     break;
  112.                 case 's':
  113.                     verbose = fopen("NIL:","w");
  114.                     if (verbose == NULL) error("no NIL");
  115.                     break;
  116.                 case 'v':
  117.                     verbose = stderr;
  118.                     break;
  119.                 default:
  120.                     error(
  121.                     "Usage: mcvert [ -[r|d|u|x|h] [D|U] [p|q] [s|v] ] filename...",
  122.                     NULL);
  123.                     }
  124.             }
  125.  
  126.         if (direction == BACKWARDS)
  127.             if (mode == HQX && unpit_flag) re_hqx();/* no re_pit() yet */
  128.             else if (mode == HQX) re_hqx();
  129.             else re_other(mode);
  130.         else
  131.             if (mode == HQX) un_hqx(unpit_flag);
  132.             else un_other(mode);
  133.         }
  134.     }
  135.  
  136. /* An array useful for CRC calculations that use 0x1021 as the "seed" */
  137. word magic[] = {
  138.     0x0000,  0x1021,  0x2042,  0x3063,  0x4084,  0x50a5,  0x60c6,  0x70e7,
  139.     0x8108,  0x9129,  0xa14a,  0xb16b,  0xc18c,  0xd1ad,  0xe1ce,  0xf1ef,
  140.     0x1231,  0x0210,  0x3273,  0x2252,  0x52b5,  0x4294,  0x72f7,  0x62d6,
  141.     0x9339,  0x8318,  0xb37b,  0xa35a,  0xd3bd,  0xc39c,  0xf3ff,  0xe3de,
  142.     0x2462,  0x3443,  0x0420,  0x1401,  0x64e6,  0x74c7,  0x44a4,  0x5485,
  143.     0xa56a,  0xb54b,  0x8528,  0x9509,  0xe5ee,  0xf5cf,  0xc5ac,  0xd58d,
  144.     0x3653,  0x2672,  0x1611,  0x0630,  0x76d7,  0x66f6,  0x5695,  0x46b4,
  145.     0xb75b,  0xa77a,  0x9719,  0x8738,  0xf7df,  0xe7fe,  0xd79d,  0xc7bc,
  146.     0x48c4,  0x58e5,  0x6886,  0x78a7,  0x0840,  0x1861,  0x2802,  0x3823,
  147.     0xc9cc,  0xd9ed,  0xe98e,  0xf9af,  0x8948,  0x9969,  0xa90a,  0xb92b,
  148.     0x5af5,  0x4ad4,  0x7ab7,  0x6a96,  0x1a71,  0x0a50,  0x3a33,  0x2a12,
  149.     0xdbfd,  0xcbdc,  0xfbbf,  0xeb9e,  0x9b79,  0x8b58,  0xbb3b,  0xab1a,
  150.     0x6ca6,  0x7c87,  0x4ce4,  0x5cc5,  0x2c22,  0x3c03,  0x0c60,  0x1c41,
  151.     0xedae,  0xfd8f,  0xcdec,  0xddcd,  0xad2a,  0xbd0b,  0x8d68,  0x9d49,
  152.     0x7e97,  0x6eb6,  0x5ed5,  0x4ef4,  0x3e13,  0x2e32,  0x1e51,  0x0e70,
  153.     0xff9f,  0xefbe,  0xdfdd,  0xcffc,  0xbf1b,  0xaf3a,  0x9f59,  0x8f78,
  154.     0x9188,  0x81a9,  0xb1ca,  0xa1eb,  0xd10c,  0xc12d,  0xf14e,  0xe16f,
  155.     0x1080,  0x00a1,  0x30c2,  0x20e3,  0x5004,  0x4025,  0x7046,  0x6067,
  156.     0x83b9,  0x9398,  0xa3fb,  0xb3da,  0xc33d,  0xd31c,  0xe37f,  0xf35e,
  157.     0x02b1,  0x1290,  0x22f3,  0x32d2,  0x4235,  0x5214,  0x6277,  0x7256,
  158.     0xb5ea,  0xa5cb,  0x95a8,  0x8589,  0xf56e,  0xe54f,  0xd52c,  0xc50d,
  159.     0x34e2,  0x24c3,  0x14a0,  0x0481,  0x7466,  0x6447,  0x5424,  0x4405,
  160.     0xa7db,  0xb7fa,  0x8799,  0x97b8,  0xe75f,  0xf77e,  0xc71d,  0xd73c,
  161.     0x26d3,  0x36f2,  0x0691,  0x16b0,  0x6657,  0x7676,  0x4615,  0x5634,
  162.     0xd94c,  0xc96d,  0xf90e,  0xe92f,  0x99c8,  0x89e9,  0xb98a,  0xa9ab,
  163.     0x5844,  0x4865,  0x7806,  0x6827,  0x18c0,  0x08e1,  0x3882,  0x28a3,
  164.     0xcb7d,  0xdb5c,  0xeb3f,  0xfb1e,  0x8bf9,  0x9bd8,  0xabbb,  0xbb9a,
  165.     0x4a75,  0x5a54,  0x6a37,  0x7a16,  0x0af1,  0x1ad0,  0x2ab3,  0x3a92,
  166.     0xfd2e,  0xed0f,  0xdd6c,  0xcd4d,  0xbdaa,  0xad8b,  0x9de8,  0x8dc9,
  167.     0x7c26,  0x6c07,  0x5c64,  0x4c45,  0x3ca2,  0x2c83,  0x1ce0,  0x0cc1,
  168.     0xef1f,  0xff3e,  0xcf5d,  0xdf7c,  0xaf9b,  0xbfba,  0x8fd9,  0x9ff8,
  169.     0x6e17,  0x7e36,  0x4e55,  0x5e74,  0x2e93,  0x3eb2,  0x0ed1,  0x1ef0
  170.     };
  171.  
  172.  
  173. /*
  174.  * calc_crc() --
  175.  *   Compute the MacBinary II-style CRC for the data pointed to by p, with the
  176.  *   crc seeded to seed.
  177.  *
  178.  *   Modified by Jim Van Verth to use the magic array for efficiency.
  179.  */
  180. short calc_mb_crc(p, len, seed)
  181. unsigned char *p;
  182. long len;
  183. short seed;
  184. {
  185.   short hold;      /* crc computed so far */
  186.   long  i;         /* index into data */
  187.  
  188.   extern unsigned short magic[];   /* the magic array */
  189.  
  190.   hold = seed;     /* start with seed */
  191.   for (i = 0; i < len; i++, p++) {
  192.     hold ^= (*p << 8);
  193.     hold = (hold << 8) ^ magic[(unsigned char)(hold >> 8)];
  194.   }
  195.  
  196.   return (hold);
  197. } /* calc_crc() */
  198.  
  199.  
  200. /* Report a fatal error */
  201. error(msg, name)
  202. char msg[], name[];
  203. {   fprintf(stderr, msg, name);
  204.     putc('\n', stderr);
  205.     exit(1);
  206.     }
  207.  
  208. /* replace illegal Unix characters in file name */
  209. /* make sure host file name doesn't get truncated beyond recognition */
  210. unixify(np)
  211. register byte *np;
  212. {   register ulong c;
  213.     c = strlen(np);
  214.     if (c > SYSNAMELEN - 4) c = SYSNAMELEN - 4;
  215.     np[c] = '\0';
  216.     np--;
  217.     while (c = *++np)
  218.         if (c <= ' ' || c == '/' || c > '~') *np = '_';
  219.     }
  220.  
  221. /* Convert Unix time (GMT since 1-1-1970) to Mac
  222.                                     time (local since 1-1-1904) */
  223. #define MACTIMEDIFF 0x7c25b080 /* Mac time of 00:00:00 GMT, Jan 1, 1970 */
  224.  
  225. ulong time2mac(time)
  226. ulong time;
  227. {   struct timeb tp;
  228.     ftime(&tp);
  229.     return long2mac(time + MACTIMEDIFF
  230.                     - 60 * (tp.timezone - 60 * tp.dstflag));
  231.     }
  232.  
  233.  
  234. /* This procedure copies the input file to the output file, basically, although
  235.     in TEXT mode it changes LF's to CR's and in any mode it forges a Mac info 
  236.     header.  Author type for TEXT mode can come from the MAC_EDITOR environ-
  237.     ment variable if it is defined. */
  238.  
  239. un_other(mode)
  240. int mode;
  241. {   register ulong b;
  242.     register ulong nchars;
  243.     char txtfname[BINNAMELEN], binfname[BINNAMELEN];
  244.     FILE *txtfile, *binfile; 
  245.     char *suffix;
  246.     struct stat stbuf;
  247.     info_header info;
  248.     int extra_chars;
  249.     ulong dlen, rlen, mtim, ctim;
  250.     short crc, calc_mb_crc();
  251.  
  252.     if (mode == DATA) suffix = ".data";
  253.     else if (mode == RSRC) suffix = ".rsrc";
  254.     else suffix = ".text";
  255.  
  256.     while (hqxnames_left[0][0] != '-') {
  257.  
  258.         strcpy(txtfname, *hqxnames_left++);
  259.         if (!(txtfile = fopen(txtfname, "r"))) {
  260.             /* Maybe we are supposed to figure out the suffix ourselves? */
  261.             strcat(txtfname, suffix);
  262.             if (!(txtfile = fopen(txtfname, "r")))
  263.                 error("Cannot open %s", txtfname);
  264.             }
  265.  
  266.         if (stat(txtfname, &stbuf))
  267.             error("Cannot read %s", txtfname);
  268.  
  269.         /* stuff header data into the info header */
  270.         bzero(&info, sizeof(info_header));
  271.         info.nlen = strlen(txtfname);
  272.         info.nlen = (info.nlen > NAMELEN) ? NAMELEN : info.nlen;
  273.     info.name[info.nlen] = '\0';
  274.         strcpy(info.name, txtfname);           /* name */
  275.         mtim = time2mac(stbuf.st_mtime);
  276.         ctim = time2mac(stbuf.st_ctime);
  277.         bcopy(&mtim, info.mtim, 4);
  278.         bcopy(&ctim, info.ctim, 4);
  279.     info.uploadvers = '\201';
  280.     info.readvers = '\201';
  281.  
  282.         if (mode == RSRC) {
  283.             /* dlen is already zero */
  284.             rlen = long2mac(stbuf.st_size);
  285.             bcopy(&rlen, info.rlen, 4);
  286.             bcopy("APPL", info.type, 4);
  287.             bcopy("CCOM", info.auth, 4);
  288.             }
  289.         else {
  290.             dlen = long2mac(stbuf.st_size);
  291.             bcopy(&dlen, info.dlen, 4);
  292.             /* rlen is already zero */
  293.             bcopy("TEXT", info.type, 4);
  294.             if (mode == DATA) bcopy("????", info.auth, 4);
  295.             else bcopy(text_author, info.auth, 4);
  296.             }
  297.  
  298.     /* calculate CRC */
  299.     crc = calc_mb_crc(&info, 124, 0);
  300.     info.crc[0] = (char) (crc >> 8);
  301.     info.crc[1] = (char) crc;
  302.  
  303.         /* Create the .bin file and write the info to it */
  304.         sprintf(binfname, "%s/%s%s", dir, txtfname, ext);
  305.         if ((binfile = fopen(binfname, "w")) == NULL)
  306.             error("Cannot open %s", binfname);
  307.         fprintf(output,
  308.                 "Converting     %-30s type = \"%4.4s\", author = \"%4.4s\"\n",
  309.                 txtfname, info.type, info.auth);
  310.         fwrite(&info, sizeof(info), 1, binfile);
  311.  
  312.         nchars = stbuf.st_size;
  313.         extra_chars = 127 - (nchars+127) % 128;
  314.         if (mode == TEXT) while (nchars--) {
  315.             b = getc(txtfile);
  316.             if (b == LF) b = CR;
  317.             putc(b, binfile);
  318.             }
  319.         else while (nchars--) b = getc(txtfile), putc(b, binfile);
  320.  
  321.         while (extra_chars--) putc(0, binfile);
  322.         fclose(binfile);
  323.         fclose(txtfile);
  324.         }
  325.     }
  326.  
  327. /* This procedure copies the input file to the output file, basically, although
  328.     in TEXT mode it changes CR's to LF's and in any mode it skips over the Mac
  329.     info header. */
  330.  
  331. re_other(mode)
  332. int mode;
  333. {   register ulong b;
  334.     register ulong nchars;
  335.     ulong temp;
  336.     char txtfname[BINNAMELEN], binfname[BINNAMELEN];
  337.     FILE *txtfile, *binfile; 
  338.     char *suffix;
  339.     info_header info;
  340.  
  341.     if (mode == DATA) suffix = ".data";
  342.     else if (mode == RSRC) suffix = ".rsrc";
  343.     else suffix = ".text";
  344.  
  345.     while (hqxnames_left[0][0] != '-') {
  346.  
  347.         strcpy(binfname, *hqxnames_left++);
  348.         if ((binfile = fopen(binfname, "r")) == NULL) {
  349.             /* Maybe we are supposed to figure out the suffix ourselves? */
  350.             strcat(binfname, ext);
  351.             if (!(binfile = fopen(binfname, "r")))
  352.                 error("Cannot open %s", binfname);
  353.             }
  354.  
  355.         /* Read the info from the .bin file, create the output file */
  356.         fread(&info, sizeof(info), 1, binfile);
  357.         strncpy(txtfname, info.name, info.nlen);
  358.     txtfname[info.nlen] = '\0';
  359.         fprintf(output,
  360.                 "Converting     %-30s type = \"%4.4s\", author = \"%4.4s\"\n",
  361.                 txtfname, info.type, info.auth);
  362.         if ((txtfile = fopen(txtfname, "r")) == NULL) {
  363.             if ((txtfile = fopen(txtfname, "w")) == NULL)
  364.                 error("Cannot open %s", txtfname);
  365.             }
  366.         else {
  367.             fclose(txtfile);
  368.             strcat(txtfname, suffix);
  369.             if ((txtfile = fopen(txtfname, "w")) == NULL)
  370.                 error("Cannot open %s", txtfname);
  371.             }
  372.  
  373.     bcopy(info.dlen, (char *) &temp, 4);
  374.     nchars = temp;
  375.         if (mode == TEXT) while (nchars--) {
  376.             b = getc(binfile);
  377.             if (b == CR) b = LF;
  378.             putc(b, txtfile);
  379.             }
  380.         else if (mode == DATA) while (nchars--)
  381.             b = getc(binfile), putc(b, txtfile);
  382.         else {
  383.             while (nchars--) getc(binfile);
  384.         bcopy(info.rlen, (char *) &temp, 4);
  385.         nchars = temp;
  386.             while (nchars--) b = getc(binfile), putc(b, txtfile);
  387.             }
  388.  
  389.         fclose(binfile);
  390.         fclose(txtfile);
  391.         }
  392.     }
  393.